package com.symone.crawler.runnable;

import cn.hutool.core.date.DateUtil;
import cn.hutool.db.Entity;
import com.symone.crawler.config.AirBnbConfig;
import com.symone.crawler.service.BookingService;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class BookingThread implements Runnable {

    private Entity room;
    private boolean useProxy;

    public BookingThread(Entity room,boolean useProxy){
        this.room = room;
        this.useProxy = useProxy;
    }

    @Override
    public void run() {
        Map<String,String> params = new HashMap<>();

        params.put("count", String.valueOf(1));
        params.put("_format", "with_conditions");
        params.put("currency", "CNY");
        params.put("locale", "zh");
        params.put("key", AirBnbConfig.key);
        params.put("listing_id",room.getStr("id"));

        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        int month = cale.get(Calendar.MONTH) + 1;
        params.put("month", String.valueOf(month));
        params.put("year", String.valueOf(year));
        new BookingService(room,params).saveWithFetch(useProxy);

        cale.add(Calendar.MONTH,1);
        year = cale.get(Calendar.YEAR);
        month = cale.get(Calendar.MONTH) + 1;
        params.put("month", String.valueOf(month));
        params.put("year", String.valueOf(year));
        new BookingService(room,params).saveWithFetch(useProxy);
    }
}
